﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Admin.ViewModels;
using SDT.Admin.Properties;
using System.IO;

namespace SDT.Admin.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        SDTDataContext eDataContent = new SDTDataContext();
        #region GET: /Customer/ Index(int? Page)
        [Authorize()]
        public ActionResult Index()
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    //sap xep
                    string order = UserState.getUserStateFromRequest("customer.order", "order", "id").ToString();
                    string orderDir = UserState.getUserStateFromRequest("customer.orderDir", "orderDir", "desc").ToString();
                    var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("customer.PageNumber", "page", 1));
                    var pageIndex = pageNumber - 1;
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("customer.pageSize", "PageSize", 20).ToString());
                    string keyword = UserState.getUserStateFromRequest("convertPrice.Keyword", "Keyword", "").ToString();
                    // tra ve cac danh sach
                    var l =CustomerManager.GetList(keyword,order, orderDir, pageIndex, pageSize);
                    //
                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    //hien thi
                    return View(new CustomerViewIndexModels
                    {
                        Customers = l,
                        Keyword=keyword

                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion
        //-------------------------------------------------------------------------------
        //
        // GET: /Branch/Details/5
        #region GET : Branch/Details(int id)
        public ActionResult Details(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    var c = eDataContent.Customers.Where(e => e.ID == id).SingleOrDefault();
                    return View(c);
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion
        //---------------------------------------------------------------------------------
        // GET: /Branch/Create
        #region GET:Branch/Create()
        public ActionResult Create()
        {
            CustomerEditModels c = new CustomerEditModels();
            c.Cities = eDataContent.Cities.ToList();
            c.Districts = eDataContent.Districts.ToList();
            c.Sexuals = Utils.StringToDictionaryIntString(Settings.Default.CustomerSexual, ",");
            return View(c);
        }
        #endregion
        //
        // POST: /Branch/Create
        #region POST:Branch/create
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(CustomerEditModels c)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    // TODO: Add insert logic here
                    UpdateModel(c);
                    var confirmPass = Request["txtConfirmPassword"];
                    if (c.Customer.Password != confirmPass)
                    {
                        ViewData[Messages.MessageInfo] = Messages.ERR_CUS_001;
                        return View();  
                    }
                    c.Customer.CityID = Utils.ParseInt(Request["ddlCity"]);
                    c.Customer.DistrictID = Utils.ParseInt(Request["ddlDistrict"]);
                    c.Customer.SexID = Utils.ParseInt(Request["rdoSex"]);
                    c.Customer.Password = Utils.EnCryptPassword(c.Customer.Password);
                    c.Customer.AddDate = DateTime.Now;
                    c.Customer.IsLockedOut = false;
                    c.Customer.LastLoginDate = DateTime.Now;
                    c.Customer.LastPasswordChangedDate = DateTime.Now;
                    c.Customer.LoweredEmail = c.Customer.Email.ToLower();
                    c.Customer.LoweredUserName = c.Customer.UserName.ToLower();
                    c.Customer.ActiveLink = Guid.NewGuid().ToString();
                    eDataContent.Customers.InsertOnSubmit(c.Customer);
                    eDataContent.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion
        //------------------------------------------------------------------------
        //
        // GET: /Branch/Edit/5

        public ActionResult Edit(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    CustomerEditModels c = new CustomerEditModels();
                    c.Customer = eDataContent.Customers.Where(e => e.ID == id).SingleOrDefault();
                    c.Cities = eDataContent.Cities.ToList();
                    c.Districts = eDataContent.Districts.ToList();
                    c.Sexuals = Utils.StringToDictionaryIntString(Settings.Default.CustomerSexual, ",");
                    return View(c);
                }                
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }

        }

        //
        // POST: /Branch/Edit/5
        #region
        [HttpPost]
        public ActionResult Edit(int id, CustomerEditModels c)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    c.Customer = eDataContent.Customers.Where(e => e.ID == id).SingleOrDefault();
                    UpdateModel(c);
                    c.Customer.CityID = Utils.ParseInt(Request["ddlCity"]);
                    c.Customer.DistrictID = Utils.ParseInt(Request["ddlDistrict"]);
                    c.Customer.SexID = Utils.ParseInt(Request["rdoSex"]);
                    c.Customer.IsLockedOut = Request["chkLock"] != "false" ? true : false;
                    eDataContent.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion
        //
        // GET: /Branch/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {
                    var item = eDataContent.Customers.Single(e => e.ID == id);
                    return View(item);
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /Branch/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.UserSystem))
                {

                    var item = eDataContent.Customers.Single(e => e.ID == id);

                    if (item != null)
                    {
                        eDataContent.Customers.DeleteOnSubmit(item);
                        eDataContent.SubmitChanges();
                    }
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

    }
}
